package com.google.ant.extensions.types;

import java.io.File;
import java.util.StringTokenizer;

public class Dependency {
    public static final String PRJ_DEP_NAME = "project.dependencies";
    
    private String groupid;
    private String artifactid;
    private String version;


    public void setGroupid(String gid) {
        this.groupid = gid;
    }

    public void setArtifactid(String aid) {
        this.artifactid = aid;
    }

    public void setVersion(String v) {
        this.version = v;
    }

    public String getGroupid() {
        return groupid;
    }

    public String getArtifactid() {
        return artifactid;
    }

    public String getVersion() {
        return version;
    }

    public boolean isSameArtifact(final Dependency d) {
        return getGroupid().equals(d.getGroupid()) 
            && getArtifactid().equals(d.getArtifactid());
    }

    public boolean isSameVersion(final Dependency d) {
        return getVersion().equals(d.getVersion());
    }

    public String getArtifact() {
        StringBuffer sb = new StringBuffer();
        sb.append(getArtifactid());
        sb.append("-");
        sb.append(getVersion());
        sb.append(".jar");
        return sb.toString();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(getGroupid().replace(".", "/"));
        sb.append("/");
        sb.append(getArtifactid());
        sb.append("/");
        sb.append(getVersion());
        sb.append("/");
        sb.append(getArtifact());
        return sb.toString();
    }

    // to support dev.java.net
    // i.e https://maven-repository.dev.java.net/nonav/repository/com.sun.xml.bind/jars/
    public String simpleForm() {
        StringBuffer sb = new StringBuffer();
        sb.append(getGroupid());
        sb.append("/jars/");
        sb.append(getArtifact());
        return sb.toString();
    }

    public int hashCode() {
        return getArtifact().hashCode();
    }

    public boolean equals(Object dep) {
        if (dep == null || getArtifact() == null) {
            return false;
        }
        if (dep instanceof Dependency) {
            return getArtifact().equals(((Dependency)dep).getArtifact());
        }
        if (dep instanceof String) {
            return getArtifact().equals(dep.toString());
        }
        if (dep instanceof File) {
            return getArtifact().equals(((File)dep).getName());
        }
        return false;
    }

    public static Dependency parse(String line) {
        StringTokenizer stoken = new StringTokenizer(line, Module.PRJ_SEP);
        Dependency dep = new Dependency();
        dep.setGroupid(stoken.nextToken());
        dep.setArtifactid(stoken.nextToken());
        dep.setVersion(stoken.nextToken());
        return dep;
    }

    public String getSignature() {
        StringBuffer sb = new StringBuffer();
        sb.append(groupid);
        sb.append(Module.PRJ_SEP);
        sb.append(artifactid);
        sb.append(Module.PRJ_SEP);
        sb.append(version);
        return sb.toString();
    }
}
